<%@ Page Language="VB" Inherits="CMS" MasterPageFile="~/admin/admin.master" ValidateRequest="false" MaintainScrollPositionOnPostback="True" Culture="auto" meta:resourcekey="PageResource1" UICulture="auto" %>

<%@ Import Namespace="System.XML" %>
<%@ Import Namespace="System.IO" %>
<%@ OutputCache Duration="1" Location="None" varybyparam="None"  %>
<script runat="server">
    'Version:       14.07.2007 /DH  Menu() mehrere Sitemaps werden erzeugt.
    'Version:       13.07.2007 /DH  Cookie LastListFilter eingefügt http://www.microsoft.com/germany/msdn/library/net/aspnet/GrundlagenZuCookiesInASPNET.mspx?mfr=true
    'Version:       11.07.2007 /BJ  Neues tbpages.Update mit Parameter
    'Version:       09.07.2007 /DH  Menü modifiziert
    'Version:       30.06.2007 /DH  Filter umgebaut
    'Version:       05.06.2007 /DH  Anlegen neuer Seite pagetyp hinzugefügt und Templatesystem entfernt.
    'Version:       05.06.2007 /DH  Web.sitmap und XML Sitemaps in den Ordner userfiles/sitemaps verlegt
    'Version:       05.06.2007 /DH  Seitliches Menü nach oben versetzt und Panels eingefügt
    'Version:       26.05.2007 /BJ  Menu, Sitemap und Versionsupdate mit einem Button "Aktualisieren"
    
    Dim SortField As String
    
    Sub Page_Load(sender As Object, e As EventArgs)
        If Not Roles.IsUserInRole("admin") Then
            Response.Redirect("~/login.aspx")
        End If

        Dim newpage As Integer = Request.QueryString("newpage")
        Select Case newpage
            Case 1
                tbPages.Insert(Titel:="*Neue Seite", _
                               homepage:="0", _
                               pagetyp:="Page")
                
            Case 2
                tbPages.Insert(Titel:="*Neue  News", _
                               homepage:="0", _
                               pagetyp:="News")
            Case 3
                tbPages.Insert(Titel:="*Neuer Termin", _
                               homepage:="0", _
                               pagetyp:="Termin")
            Case 4
                tbPages.Insert(Titel:="*Neue HTML Vorlage", _
                               homepage:="0", _
                               pagetyp:="htmlsnip")
                
                
        End Select
        If newpage > 0 Then
            'Request.QueryString("newpage")= ""
            
            'DH Function Templatesystem für Contentseiten
            'With tbTemplates.GetFelder(0, "Tnr > 0").Item(0)  ' zufällig 1.
            '    tbPages.Insert(Titel:=.Item("titel").ToString, _
            '                   Content:=.Item("content").ToString, _
            '                   homepage:=.Item("homepage").ToString)
            'End With
            Response.Redirect("~/admin/list.aspx")
        End If

        If Not IsPostBack() Then
            If SortField = "" Then
                SortField = "CNR"
            End If

            BindGrid()
            BindFilter()
            BindPagetypAuswahl()
            BindPagetypInsert()
            'Disabled SetFilter because of an reload of a page the previous filter should be hold.
            'SetFilter()
            Laden()
        End If
    End Sub

    Sub Laden()
        'Dim strFilename As String
        Dim arrMaster As New ArrayList
        Dim arrTheme As New ArrayList
        
        For Each entry As String In Directory.GetFiles(MapPath("~/" & ConfigurationManager.AppSettings("publicpath").ToString), "*.master")
            'strFilename = DisplayFileSystemInfoAttributes(New FileInfo(entry))
            'If strFilename.Contains(".master") Then arrMaster.Add(strFilename)
            arrMaster.Add(entry.Substring(entry.LastIndexOf("\") + 1, entry.Length - entry.LastIndexOf("\") - 1))
        Next
            
        Me.ddMaster.DataSource = arrMaster
        Me.ddMaster.DataBind()
        Me.ddMaster.Items.Add("")
            
        For Each entry As String In Directory.GetDirectories(MapPath("~/App_Themes/"))
            arrTheme.Add(entry.Substring(entry.LastIndexOf("\") + 1, entry.Length - entry.LastIndexOf("\") - 1))
        Next
        Me.ddTheme.DataSource = arrTheme
        Me.ddTheme.DataBind()
        Me.ddTheme.Items.Add("")
                       
       
        Me.ddPagetyp2.DataSource = tbPages.GetListPageTyp
        ddPagetyp2.DataBind()
       
        Me.ddSprachen.DataSource = tbPages.GetSprachen
        ddSprachen.DataBind()
        ddSprachen.Items.Add("")
        
    End Sub
    Sub BindPagetypAuswahl()
        ddPagetyp.Items.Clear()
        ddPagetyp.Items.Add(" ")
        Me.ddPagetyp.DataSource = tbPages.GetListPageTyp
        ddPagetyp.DataBind()
        ddPagetyp.Items.Remove("")
        
        Dim strPagetyp As String = Me.GetcookieLastListFilter
        If strPagetyp.Contains("pagetyp=") Then
            strPagetyp = strPagetyp.Replace("pagetyp=", "")
            strPagetyp = strPagetyp.Replace("'", "")
            strPagetyp = strPagetyp.Replace("OR pagetyp Is Null", "")
            strPagetyp = strPagetyp.Replace(" ","")
        End If
        Dim TempPagetyp As New ListItem
        
        TempPagetyp.Text = strPagetyp
        If ddPagetyp.Items.Contains(TempPagetyp) Then
            ddPagetyp.SelectedValue = strPagetyp
        Else
            ddPagetyp.SelectedValue = " "
        End If
       
        
    End Sub

    Sub BindFilter()
        ddFiltergesp.Items.Clear()
        Dim LeereItem As New ListItem
        LeereItem.Text = " "
        LeereItem.Value = " "
        
        'Me.ddFiltergesp.DataSource = ""
        Dim dsFilterListe As New DataSet
        dsFilterListe = Me.tbPages.GetDataSet(strWhere:="pagetyp='Filter'")
        Me.ddFiltergesp.DataSource = dsFilterListe
        Me.ddFiltergesp.DataSource = dsFilterListe.Tables(0)
        Me.ddFiltergesp.DataMember = "cnr"
         
        Me.ddFiltergesp.DataValueField = dsFilterListe.Tables(0).Columns("Content").ColumnName.ToString
        Me.ddFiltergesp.DataTextField = dsFilterListe.Tables(0).Columns("Titel").ColumnName.ToString
        Me.ddFiltergesp.DataBind()
        ddFiltergesp.Items.Add(LeereItem)
    End Sub
    
    
    Sub BindPagetypInsert()
        Dim arrl As ArrayList
        Dim str As String
        
        arrl = tbPages.GetListPageTyp
        For Each arr In arrl
            str = arr.ToString
        Next
        ddpage.Items.Clear()
        Me.ddpage.DataSource = tbPages.GetListPageTyp
        ddpage.DataBind()
        ddpage.Items.Remove("")
    End Sub

    Sub MyDataGrid_Sort(ByVal sender As Object, ByVal e As DataGridSortCommandEventArgs)
        SortField = e.SortExpression
        lblSortierung.Text = SortField
        Me.SetCookieLastListOrder(SortField)
        SortField = Me.GetcookieLastListOrder
        Me.BindGrid("", SortField)
    End Sub

    Sub BindGrid(Optional ByVal strWhere As String = "", Optional ByVal strOrder As String = "")
        'Dim strWhere As String
        'Dim strOrder As String
        If strWhere <> "" Then
            SetCookieLastListFilter(strWhere)
        Else
            strWhere = GetcookieLastListFilter()
            
        End If
        If strOrder <> "" Then
            Me.SetCookieLastListOrder(strOrder)
            
        Else
            strOrder = Me.GetcookieLastListOrder
        End If
        
        
        'strWhere = Me.GetcookieLastListFilter
        
        'strOrder = GetcookieLastListOrder()
        
        MyDataGrid.DataSource = tbPages.GetDataSet(strOrder:=strOrder, strWhere:=strWhere).Tables(0).DefaultView
        MyDataGrid.DataBind()
    End Sub
    
    'Sub BindGridohneCookie(ByVal strWhere As String, ByVal strOrder As String)
    '    If strWhere <> "" Then
    '        SetCookieLastListFilter(strWhere)
    '    Else
    '        strWhere = GetcookieLastListFilter()
            
    '    End If
    '    If strOrder <> "" Then
    '        Me.SetCookieLastListOrder(strOrder)
            
    '    Else
    '        strOrder = Me.GetcookieLastListOrder
    '    End If
        
    '    MyDataGrid.DataSource = tbPages.GetDataSet(strOrder:=strOrder, strWhere:=strWhere).Tables(0).DefaultView
    '    MyDataGrid.DataBind()
    'End Sub
    
    Sub MyDataGrid_Page(ByVal sender As Object, ByVal e As DataGridPageChangedEventArgs)
        MyDataGrid.CurrentPageIndex = e.NewPageIndex
        BindGrid()
    End Sub

    Private Function FindNode(ByVal pNode As XmlNode, ByVal id As String, ByVal Ebene As Integer) As XmlNode
        If pNode.HasChildNodes Then
            Dim node As XmlNode = pNode.FirstChild
            Do Until node Is Nothing
                If Microsoft.VisualBasic.Left(node.Attributes.GetNamedItem("id").Value, Ebene) = Microsoft.VisualBasic.Left(id, Ebene) Then
                    If Ebene = id.Length Then Exit Do
                    pNode = FindNode(node, id, Ebene + 1) : Exit Do
                Else
                    node = node.NextSibling()
                End If
            Loop
        End If
        FindNode = pNode
    End Function

    Private Sub AddNode(ByVal MyXml As XmlDocument, ByVal id$, ByVal title$, ByVal url$, ByVal desc$)
        Dim node As XmlElement
        Dim newnode As XmlElement

        id = Replace(id, ",", "")
        newnode = MyXml.CreateElement("siteMapNode")
        newnode.SetAttribute("id", id)
        newnode.SetAttribute("title", title)
        newnode.SetAttribute("url", url)
        newnode.SetAttribute("description", desc)
        node = FindNode(MyXml.DocumentElement, id, 1)
        If Not IsNothing(node) Then node.AppendChild(newnode)
    End Sub

    Sub BtnAktualisieren(ByVal sender As Object, ByVal e As EventArgs)
        Menu()
        Sitemap()
        Dim Version As New bj.Version
        Version.ConnString = ConfigurationManager.ConnectionStrings("mastercms").ConnectionString
        Version.Update()
    End Sub
    
    Private Sub Menu()
        ' XML-erzeugen        
        'Dim myXML As New XmlDocument
        'Dim decl As XmlDeclaration = myXML.CreateXmlDeclaration("1.0", "utf-8", "")
        'myXML.InsertBefore(decl, myXML.DocumentElement)
        'Dim rootEle As XmlElement = myXML.CreateElement("siteMap")
        ''rootEle.SetAttribute("siteMap", "http://schemas.microsoft.com/AspNet/SiteMap-File-1.0>")
        'rootEle.SetAttribute("xmlns", "http://schemas.microsoft.com/AspNet/SiteMap-File-1.0>")
        'myXML.AppendChild(rootEle)
        
        Dim Row1 As DataRow
        Dim Row As DataRow
        Dim myXML As New XmlDocument
        Dim decl As XmlDeclaration
        Dim rootEle As XmlElement
        Dim i As Integer = 1
        For Each Row1 In tbPages.GetDataSet(strWhere:="menuitem Like '[A-Z]'", strOrder:="menuitem").Tables(0).Rows
            decl = myXML.CreateXmlDeclaration("1.0", "utf-8", "")
            myXML.InsertBefore(decl, myXML.DocumentElement)
            rootEle = myXML.CreateElement("siteMap")
            rootEle.SetAttribute("xmlns", "http://schemas.microsoft.com/AspNet/SiteMap-File-1.0")
            myXML.AppendChild(rootEle)
            
            ' menuitems eintragen
            'For Each Row In tbPages.GetDataSet(strWhere:="menuitem Is Not Null", strOrder:="menuitem").Tables(0).Rows
            For Each Row In tbPages.GetDataSet(strWhere:="menuitem Like'" & Row1("menuitem").ToString() & "%'", strOrder:="menuitem").Tables(0).Rows
                'strWhere:="menuitem Like '" & strMenuitem & ",_'",
                If Row("menuitem").ToString <> "" Then
                    If Row("linkurl").ToString <> "" Then
                        AddNode(myXML, Row("menuitem").ToString, Row("linktext").ToString, Row("linkurl").ToString, Row("metadescription").ToString)
                    Else
                        'AddNode( myXml, myReader("menuitem").ToString, myReader("linktext").ToString, "default.aspx?cms=" & myReader("cnr").ToString, myReader("metadescription").ToString )
                        'URLRewrite verwenden
                        AddNode(myXML, Row("menuitem").ToString, Row("linktext").ToString, Row("linktext").ToString & "-" & Row("cnr").ToString & ".aspx", Row("metadescription").ToString)
                    End If
                End If
            Next
        
            'XML speichern
            'myXML.Save(MapPath("../web.sitemap"))
            If i = 1 Then
                myXML.Save(MapPath("../userfiles/sitemaps/default.sitemap"))
                myXML.Save(MapPath("../userfiles/sitemaps/" & Row1("menuitem").ToString() & ".sitemap"))
            Else
                myXML.Save(MapPath("../userfiles/sitemaps/" & Row1("menuitem").ToString() & ".sitemap"))
            End If
            myXML.RemoveAll()
            i = i + 1
        Next
    End Sub

    Private Sub Sitemap()
        'Sitemaps werden im Ordner publicpath erzeugt. 
        Dim myUrl As String = Me.Context.Request.Url.Host & Me.Context.Request.ApplicationPath & "/"

        Dim arrLinks(tbPages.Count("sitemap = Yes"), 8) As String
        Dim Row As DataRow
        Dim i As Integer = 0
        Dim strMetatmp As String
        
        For Each Row In tbPages.GetDataSet(strWhere:="sitemap = Yes", strOrder:="linktext").Tables(0).Rows
            strMetatmp = ""
            arrLinks(i, 0) = Row("cnr").ToString
            arrLinks(i, 1) = Row("menuitem").ToString
            arrLinks(i, 2) = Server.HtmlDecode(Row("linktext").ToString)
            strMetatmp = Server.HtmlDecode(Row("metadescription").ToString).Replace("<", "")
            strMetatmp = strMetatmp.Replace(">", "")
            arrLinks(i, 3) = strMetatmp
            arrLinks(i, 4) = Row("linkurl").ToString
            arrLinks(i, 5) = Row("sitemap").ToString
            arrLinks(i, 6) = Row("creationdate").ToString
            arrLinks(i, 7) = Row("updatedate").ToString
            i = i + 1
        Next
        
        'Googlesitemap erzeugen
        'Dateiname gsitemap.xml
        '<?xml version="1.0" encoding="UTF-8"?>
        '< urlset xmlns="http://www.google.com/schemas/sitemap/0.84">
        '< url>
        '< loc>http://www.example.com/</loc>
        '< lastmod>2005-01-01</lastmod>
        '< changefreq>monthly</changefreq>
        '< priority>0.8</priority>
        '</url>  
        '</urlset>
        'Für Google,Microsoft und Yahoo http://www.sitemaps.org/protocol.html
        '<?xml version="1.0" encoding="UTF-8"?>
        '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
        '   <url>
        '       <loc>http://www.example.com/</loc>
        '       <lastmod>2005-01-01</lastmod>
        '       <changefreq>monthly</changefreq>
        '       <priority>0.8</priority>
        '   </url>
        '</urlset> 

        Dim strGSitemap As String = "<?xml version=""1.0"" encoding=""UTF-8""?><urlset xmlns=""http://www.google.com/schemas/sitemap/0.84"">" & vbCrLf
    
        For j As Integer = 0 To UBound(arrLinks) - 1
            strGSitemap = strGSitemap & "<url>"
       
            If arrLinks(j, 4) <> "" Then
                'externe Links nicht aufnehmen
                If Not InStr(arrLinks(j, 4), "http://") > 0 Then
                    strGSitemap = strGSitemap & "<loc>" & "http://" & myUrl & arrLinks(j, 4) & "</loc>"
                End If
            Else
                strGSitemap = strGSitemap & "<loc>" & "http://" & myUrl & arrLinks(j, 2) & "-" & arrLinks(j, 0) & ".aspx</loc>"
            End If
       
            If arrLinks(j, 7) <> "" Then
                strGSitemap = strGSitemap & "<lastmod>" & datecorrect(arrLinks(j, 7).Replace(" 00:00:00", "")) & "</lastmod>"
            End If
            strGSitemap = strGSitemap & "<changefreq>weekly</changefreq>"
            strGSitemap = strGSitemap & "<priority>0.8</priority>"
            strGSitemap = strGSitemap & "</url>" & vbCrLf
       
        Next
        strGSitemap = strGSitemap & "</urlset>"
        Dim FiStr As FileStream = _
                      New FileStream(Me.Context.Request.PhysicalApplicationPath & "\" & "userfiles\sitemaps\gSitemap.xml", FileMode.Create)
        'New FileStream(Me.Context.Request.PhysicalApplicationPath & ConfigurationManager.AppSettings("publicpath") & "\" & "gSitemap.xml", FileMode.Create)
        Dim StrWr As StreamWriter = New StreamWriter(FiStr)
        StrWr.WriteLine(strGSitemap)
        StrWr.Close()
        'Sitemap
        Dim strSitemap As String = "<?xml version=""1.0"" encoding=""UTF-8""?><siteMap xmlns=""http://schemas.microsoft.com/AspNet/SiteMap-File-1.0"">" & vbCrLf
    
        For j As Integer = 0 To UBound(arrLinks) - 1
            strSitemap = strSitemap & "<siteMapNode"
       
            If arrLinks(j, 4) <> "" Then
                'externe Links nicht aufnehmen
                If Not InStr(arrLinks(j, 4), "http://") > 0 Then
                    strSitemap = strSitemap & " url=""" & Server.UrlEncode(arrLinks(j, 4)) & """"
                End If
            Else
                strSitemap = strSitemap & " url=""" & Server.UrlEncode(arrLinks(j, 2)) & "-" & arrLinks(j, 0) & ".aspx"""
            End If
            strSitemap = strSitemap & " cnr=""" & arrLinks(j, 0) & """"
            strSitemap = strSitemap & " menuitem=""" & arrLinks(j, 1) & """"
            strSitemap = strSitemap & " linktext=""" & arrLinks(j, 2) & """"
            strSitemap = strSitemap & " metadescription=""" & arrLinks(j, 3).Replace(Chr(34), "") & """"
            strSitemap = strSitemap & " creationdate=""" & arrLinks(j, 6) & """"
            strSitemap = strSitemap & " lastmod=""" & arrLinks(j, 7) & """"
            strSitemap = strSitemap & " />" & vbCrLf
       
        Next
        strSitemap = strSitemap & "</siteMap>"
        Dim FiStr2 As FileStream = _
                        New FileStream(Me.Context.Request.PhysicalApplicationPath & "\" & "userfiles\sitemaps\Sitemap.xml", FileMode.Create)
        'New FileStream(Me.Context.Request.PhysicalApplicationPath & ConfigurationManager.AppSettings("publicpath") & "\" & "Sitemap.xml", FileMode.Create)
        Dim StrWr2 As StreamWriter = New StreamWriter(FiStr2)
        StrWr2.WriteLine(strSitemap)
        StrWr2.Close()
    
    End Sub

    Private Function datecorrect(ByVal strDate As String) As String
        'DH 6.12.2006
        'Wandelt das Datum 16.10.2005 in 2005-10-16 um. 
        'Wird für Google Sitemap Datumsformat benötigt.
        Dim arrdate As Array
        arrdate = strDate.Split(".")
        strDate = arrdate(2) & "-" & arrdate(1) & "-" & arrdate(0)
        Return strDate
    End Function
    
    Sub btnUpdateMeta_click(ByVal sender As Object, ByVal e As EventArgs)
        Dim i As Integer
        Dim _item As DataGridItem
        
        For i = 0 To MyDataGrid.Items.Count - 1

            _item = MyDataGrid.Items(i)
            Dim itCheckBox As CheckBox = _item.FindControl("myCheck")
            Dim txtCNR As TextBox = _item.FindControl("cnrid")
            
            If itCheckBox.Checked = True Then
                Dometaupdate(txtCNR.Text)
            End If
        
        Next
        BindGrid()
    End Sub
    
    Sub btnLinksetzen_click(ByVal sender As Object, ByVal e As EventArgs)
        Dim i As Integer
        Dim _item As DataGridItem
        
        For i = 0 To MyDataGrid.Items.Count - 1

            _item = MyDataGrid.Items(i)
            Dim itCheckBox As CheckBox = _item.FindControl("myCheck")
            Dim txtCNR As TextBox = _item.FindControl("cnr")
            
            If itCheckBox.Checked = True Then
                setzelink(txtCNR.Text)
            End If
        
        Next
        BindGrid()
    End Sub
    
    Sub btnSitemapAddDel_click(ByVal sender As Object, ByVal e As EventArgs)
        Dim i As Integer
        Dim _item As DataGridItem
        Dim updval As Integer
        For i = 0 To MyDataGrid.Items.Count - 1
            _item = MyDataGrid.Items(i)
            Dim itCheckBox As CheckBox = _item.FindControl("myCheck")
            Dim txtCNR As TextBox = _item.FindControl("cnrid")
            Dim itSitemapBox As CheckBox = _item.FindControl("mySitemap")
            If itCheckBox.Checked = True Then
                If itSitemapBox.Checked Then
                    updval = 0
                Else
                    updval = 1
                End If
                tbPages.Update(CInt(txtCNR.Text), "sitemap", updval)
            End If
        Next
        BindGrid()
    End Sub
    
    Sub btnLoeschen_click(ByVal sender As Object, ByVal e As EventArgs)
        Dim i As Integer
        Dim _item As DataGridItem
        For i = 0 To MyDataGrid.Items.Count - 1
            _item = MyDataGrid.Items(i)
            Dim itCheckBox As CheckBox = _item.FindControl("myCheck")
            Dim txtCNR As TextBox = _item.FindControl("cnrid")
            
            If itCheckBox.Checked = True Then
                tbPages.Delete(CInt(txtCNR.Text))
            End If
        Next
        BindGrid()
    End Sub
    
    Private Sub setzelink(ByVal intCnr As Integer)
        'Funktion:  Wandelt den Content einer Page die Keywords in Links um
        'Autor:     DH
        'Datum:     01.08.2006
        '1. Content holen
        '2. alte Links entfernen
        '3. Linktexte holen nach spaces absteigend sortiert, neue Links setzen
        '4. In DB zurück schreiben
        
        '1. Content holen
        Dim mypage As String = tbPages.GetFelder(intCnr).Item(0).Item("content").ToString

        '2. alte Links entfernen
        '<a class="autolink" href="default.aspx">test</a><!--autolink-->
        mypage = RemoveAutolink(mypage)

        '3. Linktexte holen und einfügen
        Dim strNeuerLink As String
        Dim count As Integer = -1
        Dim Row As DataRow
        For Each Row In tbPagesZusatz.GetDataSet(strWhere:="CNr <> " & intCnr, strOrder:="spaces DESC").Tables("Table").Rows
            If Row("linkurl").ToString <> "" Then
                strNeuerLink = "<a class=""autolink"" href=""" & Row("linkurl").ToString & """>" & Row("linktexte").ToString.Replace(" ", "&nbsp;") & "</a><!--autolink-->"
            Else
                strNeuerLink = "<a class=""autolink"" href=""" & Row("linktexte").ToString & "-" & Row("cnr").ToString & ".aspx"">" & Row("linktexte").ToString.Replace(" ", "&nbsp;") & "</a><!--autolink-->"
            End If
            mypage = InsertAutolink(mypage, Row("linktexte").ToString, strNeuerLink)
        Next
        'In Datenbank zurück schreiben
        tbPages.Update(intCnr, "Content", mypage)
    End Sub
    
    Protected Sub btnSaveFiler_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        If Me.txtFilter.Text <> "" Then
            Me.tbPages.Insert(Titel:=Me.txtFilter.Text, Content:=Me.txtFilter.Text, pagetyp:="Filter")
        End If
        BindFilter()
        BindPagetypAuswahl()
        
    End Sub

  

    Protected Sub btnFilter_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        If Me.txtFilter.Text <> "" Then
            BindGrid(Me.txtFilter.Text, "")
        End If
    End Sub
    
    Protected Sub ddFiltergesp_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        If ddFiltergesp.SelectedValue = " " Then
            BindGrid()
        Else
            BindGrid(ddFiltergesp.SelectedValue, "")
        End If
        ddPagetyp.SelectedValue = " "
    End Sub
    
    Protected Sub ddPagetyp_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        'Dim strWhere As String
        'Select Case Me.ddPagetyp.SelectedValue
        '    Case ("Alle")
        '        strWhere = Nothing
        '    Case ("Artikel")
        '        strWhere = "pagetyp='Artikel' OR pagetyp Is Null"
        '    Case Else
        '        strWhere = "pagetyp='" & Me.ddPagetyp.SelectedValue & "'"
        'End Select
        'If strWhere <> "" Then
        '    BindGridohneCookie(strWhere, "")
        'End If
        SetFilter()
        ddFiltergesp.SelectedValue = " "
    End Sub

    Protected Sub SetFilter()
        Dim strWhere As String
        Select Case Me.ddPagetyp.SelectedValue
            Case " "
                strWhere = "pagetyp Like '%'"
            Case ("")
                strWhere = Nothing
            Case ("Page")
                strWhere = "pagetyp='Page' OR pagetyp Is Null"
            Case Else
                strWhere = "pagetyp='" & Me.ddPagetyp.SelectedValue & "'"
        End Select
        If strWhere <> "" Then
            SetCookieLastListFilter(strWhere)
            BindGrid(strWhere, "")
        Else
            
        End If
    End Sub
   
    Sub SetCookieLastListFilter(ByVal strWhere As String)
        Response.Cookies("lastListFilter").Value = strWhere
        Response.Cookies("lastListFilter").Expires = DateTime.Now.AddDays(150)
    End Sub
    
    Private Function GetcookieLastListFilter() As String
        If Not Request.Cookies("lastListFilter") Is Nothing Then
            GetcookieLastListFilter = Request.Cookies("lastListFilter").Value
        Else
            GetcookieLastListFilter = "creationdate=CVDate('" & CDate(Date.Today) & "')"
        End If
    End Function
    
    Sub SetCookieLastListOrder(ByVal strOrder As String)
        Dim strLastfilter As String = GetcookieLastListOrder()

        If strLastfilter.Contains("DESC") Then
            strOrder = strOrder.Replace("DESC", "")
            strOrder = strOrder & " ASC"
        Else
            strOrder = strOrder.Replace("ASC", "")
            strOrder = strOrder & " DESC"
        End If
                
        Response.Cookies("lastListOrder").Value = strOrder
        Response.Cookies("lastListOrder").Expires = DateTime.Now.AddDays(150)
    End Sub
    

    Private Function GetcookieLastListOrder() As String
        If Not Request.Cookies("lastListOrder") Is Nothing Then

            GetcookieLastListOrder = Request.Cookies("lastListOrder").Value
            
            'to avoid that you get an error wenn DESC and ASC is set at the same time
            If GetcookieLastListOrder.Contains("DESC ASC") Then
                GetcookieLastListOrder = GetcookieLastListOrder.Replace("DESC ASC", "")
            End If

            If GetcookieLastListOrder.Contains("ASC DESC") Then
                GetcookieLastListOrder = GetcookieLastListOrder.Replace("ASC DESC", "")
            End If
        Else
            GetcookieLastListOrder = "CNR DESC"
        End If
    End Function

    Protected Sub btnadd2_Click(sender As Object, e As EventArgs)
        tbPages.Insert(Titel:="*New " & Me.ddpage.SelectedValue, _
                            homepage:="0", _
                            pagetyp:=Me.ddpage.SelectedValue)
        Response.Redirect("~/admin/list.aspx")
    End Sub

    Protected Sub btnaktual2_Click(sender As Object, e As EventArgs)
        Menu()
        Sitemap()
        Dim Version As New bj.Version
        Version.ConnString = ConfigurationManager.ConnectionStrings("mastercms").ConnectionString
        Version.Update()
    End Sub
   
    
    Protected Sub btnfilter2_Click(sender As Object, e As EventArgs)
        SetFilter()
    End Sub

    Protected Sub btnfiltercnr_Click(sender As Object, e As EventArgs)
        Me.txtFilter.Text = "CNR="
    End Sub

    Protected Sub btnfiltertitel_Click(sender As Object, e As EventArgs)
        Me.txtFilter.Text = "Titel like '%searchword%'"
    End Sub

    Protected Sub btnfiltercontent_Click(sender As Object, e As EventArgs)
        Me.txtFilter.Text = "Content like '%searchword%'"
    End Sub

    Protected Sub btnfiltercreationdate_Click(sender As Object, e As EventArgs)
        Me.txtFilter.Text = "Creationdate > #12/31/2013#"
    End Sub

  
    Protected Sub btnCss_Click(sender As Object, e As EventArgs)
        Dim oDir As IO.DirectoryInfo
        oDir = New IO.DirectoryInfo(Server.MapPath("~/userfiles/"))
 
        ' alle Dateien des Ordners ermitteln 
        ' und in der ListBox anzeigen
        Getfiles(oDir, "*.css")
        
        Getfiles(oDir, "*.master")
        
        Getfiles(oDir, "*.js")

        '        'Me.GetFiles(Server.MapPath("~/userfiles/"), "*.css")
        oDir = New IO.DirectoryInfo(Server.MapPath("~/App_Themes/"))
        Getfiles(oDir, "*.skin")
        Getfiles(oDir, "*.css")
        Response.Redirect("~/admin/list.aspx")
        
        
    End Sub

    Protected Sub btnMaster_Click(sender As Object, e As EventArgs)
        Dim i As Integer
        Dim _item As DataGridItem
        Dim updval As String
        
        For i = 0 To MyDataGrid.Items.Count - 1

            _item = MyDataGrid.Items(i)
            Dim itCheckBox As CheckBox = _item.FindControl("myCheck")
            Dim txtCNR As TextBox = _item.FindControl("cnrid")
            
            If itCheckBox.Checked = True Then
                updval = Me.ddMaster.SelectedValue
                tbPages.Update(CInt(txtCNR.Text), "mastervorlage", updval)
            End If
        
        Next
        BindGrid()
    End Sub

    Protected Sub btnTheme_Click(sender As Object, e As EventArgs)
        Dim i As Integer
        Dim _item As DataGridItem
        Dim updval As String
        
        For i = 0 To MyDataGrid.Items.Count - 1

            _item = MyDataGrid.Items(i)
            Dim itCheckBox As CheckBox = _item.FindControl("myCheck")
            Dim txtCNR As TextBox = _item.FindControl("cnrid")
            
            If itCheckBox.Checked = True Then
                updval = Me.ddTheme.SelectedValue
                tbPages.Update(CInt(txtCNR.Text), "theme", updval)
            End If
        
        Next
        BindGrid()
    End Sub

    Protected Sub btnSprachen_Click(sender As Object, e As EventArgs)
        Dim i As Integer
        Dim _item As DataGridItem
        Dim updval As String
        
        For i = 0 To MyDataGrid.Items.Count - 1

            _item = MyDataGrid.Items(i)
            Dim itCheckBox As CheckBox = _item.FindControl("myCheck")
            Dim txtCNR As TextBox = _item.FindControl("cnrid")
            
            If itCheckBox.Checked = True Then
                updval = Me.ddSprachen.SelectedValue
                tbPages.Update(CInt(txtCNR.Text), "sprache", updval)
            End If
        
        Next
        BindGrid()
    End Sub
    
    Protected Sub btnPagetyp2_Click(sender As Object, e As EventArgs)
        Dim i As Integer
        Dim _item As DataGridItem
        Dim updval As String
        
        For i = 0 To MyDataGrid.Items.Count - 1

            _item = MyDataGrid.Items(i)
            Dim itCheckBox As CheckBox = _item.FindControl("myCheck")
            Dim txtCNR As TextBox = _item.FindControl("cnrid")
            
            If itCheckBox.Checked = True Then
                updval = Me.ddPagetyp2.SelectedValue
                tbPages.Update(CInt(txtCNR.Text), "pagetyp", updval)
            End If
        
        Next
        BindGrid()
    End Sub

    
</script>

<asp:Content ID="Content1" runat="server" ContentPlaceHolderID="ContentPlaceHolder1">

    <div class="panel panel-danger">
        <div class="panel-heading">
            <h3 class="panel-title">
                <asp:Label ID="lbltitle" runat="server" Text="Add a new item" meta:resourcekey="lbltitleResource1"></asp:Label></h3>
        </div>
        <div class="row">
            <div class="col-xs-6 col-md-4">
                <div class="panel-body">

                    <div class="btn-group">
                        <asp:DropDownList ID="ddpage" runat="server" CssClass="form-control" meta:resourcekey="ddpageResource1"></asp:DropDownList>
                    </div>
                    <asp:Button ID="btnadd2" runat="server" class="btn btn-default" Text="Add" OnClick="btnadd2_Click" meta:resourcekey="btnadd2Resource1" />
                </div>
            </div>
            <div class="col-xs-6 col-md-4">
                <div class="panel-body">
                    <asp:Button ID="btnaktual2" runat="server" class="btn btn-default" Text="Refresh" OnClick="btnaktual2_Click" meta:resourcekey="btnaktual2Resource1" />
                </div>
            </div>
            <div class="col-xs-6 col-md-4">
                <div class="panel-body">
                    <asp:Button ID="btnCss" runat="server" class="btn btn-default" Text="Load CSS,JS,MASTER" OnClick="btnCss_Click" />
                </div>
            </div>
        </div>
    </div>

    <div class="panel panel-default">
        <div class="panel-heading">
            <h3 class="panel-title">
                <asp:Label ID="lblfilter" runat="server" Text="Filter" meta:resourcekey="lblfilterResource1"></asp:Label></h3>
        </div>
        <div class="panel-body">

            <div class="row">
                <div class="col-md-2">
                    <div class="btn-group">
                        <asp:DropDownList ID="ddPagetyp" CssClass="form-control" runat="server" AppendDataBoundItems="True" AutoPostBack="True" OnSelectedIndexChanged="ddPagetyp_SelectedIndexChanged" meta:resourcekey="ddPagetypResource1"></asp:DropDownList>
                    </div>
                </div>
                <div class="col-md-5">
                    
                        <div class="input-group">
                            <asp:TextBox ID="txtFilter" runat="server" Width="300px" CssClass="form-control" meta:resourcekey="txtFilterResource1"></asp:TextBox>
                            <span class="input-group-btn">
                                <asp:Button ID="btnFilter" runat="server" CssClass="btn btn-default" Text="Filter" OnClick="btnFilter_Click" meta:resourcekey="btnFilterResource1" />
                                <asp:Button ID="btnSaveFiler" runat="server" CssClass="btn btn-default" OnClick="btnSaveFiler_Click" Text="Save" meta:resourcekey="btnSaveFilerResource1" />
                            </span>
                        </div>
                        <!-- /input-group -->
                        <div class="btn-group btn-group-xs">
                            <asp:Button ID="btnfiltercnr" runat="server" CssClass="btn btn-default" Text="CNR" meta:resourcekey="btnfiltercnrResource1" OnClick="btnfiltercnr_Click" />
                            <asp:Button ID="btnfiltertitel" runat="server" CssClass="btn btn-default" Text="Titel" meta:resourcekey="btnfiltertitelResource1" OnClick="btnfiltertitel_Click" />
                            <asp:Button ID="btnfiltercontent" runat="server" CssClass="btn btn-default" Text="Content" meta:resourcekey="btnfiltercontentResource1" OnClick="btnfiltercontent_Click" />
                            <asp:Button ID="btnfiltercreationdate" runat="server" CssClass="btn btn-default" Text="Creationdate" meta:resourcekey="btnfiltercreationdateResource1" OnClick="btnfiltercreationdate_Click" />
                        </div>
                   

                </div>
                <div class="col-md-1"></div>

                <div class="col-md-4">

                    <div class="col-lg-6">
                        <div class="btn-group">
                            <asp:DropDownList ID="ddFiltergesp" runat="server" CssClass="form-control" AutoPostBack="True" OnSelectedIndexChanged="ddFiltergesp_SelectedIndexChanged" meta:resourcekey="ddFiltergespResource1"></asp:DropDownList>
                        </div>

                    </div>

                </div>
            </div>
        </div>
    </div>

    <div class="panel panel-success">
        <div class="panel-heading">
            <h3 class="panel-title">
                <asp:Label ID="lblpages" runat="server" Text="Pages" meta:resourcekey="lblpagesResource1"></asp:Label>
            </h3>
        </div>
        <div class="panel-body">

            <asp:DataGrid ID="MyDataGrid" runat="server" DataKeyField="CNR"
                AllowPaging="True"
                PageSize="50"
                PagerStyle-Mode="NumericPages"
                PagerStyle-NextPageText="Next"
                PagerStyle-PrevPageText="Prev"
                OnPageIndexChanged="MyDataGrid_Page"
                AutoGenerateColumns="False"
                AllowSorting="True"
                OnSortCommand="MyDataGrid_Sort" Width="100%" CssClass="table table-hover" 
                meta:resourcekey="MyDataGridResource1">
                <Columns>
                    <asp:TemplateColumn>
                        <ItemTemplate>
                            <asp:CheckBox runat="server"
                                ID="myCheck" meta:resourcekey="myCheckResource1" />
                        </ItemTemplate>
                    </asp:TemplateColumn>
                    <asp:TemplateColumn HeaderText="ID">
                        <ItemTemplate>
                            <asp:TextBox ID="cnrid" runat="server"
                                Text='<%# DataBinder.Eval(Container.DataItem, "CNR") %>'
                                Width="30px"
                                Enabled="False" meta:resourcekey="cnridResource1" />
                        </ItemTemplate>
                    </asp:TemplateColumn>
                    <asp:TemplateColumn HeaderText="S.Map" ItemStyle-Width="20">
                        <ItemTemplate>
                            <asp:CheckBox runat="server"
                                ID="mySitemap" Enabled="False" Checked='<%# DataBinder.Eval(Container.DataItem, "sitemap") %>' meta:resourcekey="mySitemapResource1" />
                        </ItemTemplate>

                        <ItemStyle Width="20px"></ItemStyle>
                    </asp:TemplateColumn>
                    <asp:BoundColumn HeaderText="Titel" SortExpression="Titel" DataField="Titel"></asp:BoundColumn>


                    <asp:BoundColumn HeaderText="Link Text" SortExpression="linktext" DataField="linktext"></asp:BoundColumn>

                    <asp:BoundColumn HeaderText="Date" DataFormatString="{0:dd.MM.yyyy}" SortExpression="creationdate" DataField="creationdate" ItemStyle-Width="20">

                        <ItemStyle Width="20px"></ItemStyle>
                    </asp:BoundColumn>

                    <asp:HyperLinkColumn
                        HeaderText="Edit"
                        DataNavigateUrlField="CNR"
                        DataNavigateUrlFormatString="edit.aspx?cms={0}"
                        Text="&lt;span class='glyphicon glyphicon-edit'&gt;&lt;/span&gt;" ItemStyle-Width="30" meta:resourcekey="HyperLinkColumnResource1">

                        <ItemStyle Width="30px"></ItemStyle>
                    </asp:HyperLinkColumn>

                </Columns>
                <PagerStyle HorizontalAlign="Right" Mode="NumericPages" NextPageText="Next" PrevPageText="Prev" />

            </asp:DataGrid>
            <asp:Label ID="lblSortierung" Font-Size="XX-Small" runat="server" meta:resourcekey="lblSortierungResource1"></asp:Label>

        </div>
        <div class="panel-footer">
            <asp:Label ID="lblselecteditems" runat="server" Text="Selected items"></asp:Label>
            <asp:LinkButton ID="btnUpdate" runat="server"
                Text="Update metadata"
                Font-Names="Verdana"
                Font-Size="8pt"
                OnClick="btnUpdateMeta_click" meta:resourcekey="btnUpdateResource1" />
            ::
                            <asp:LinkButton ID="btnLinksetzen" runat="server"
                                Text="Set links"
                                Font-Names="Verdana"
                                Font-Size="8pt"
                                OnClick="btnLinksetzen_click" meta:resourcekey="btnLinksetzenResource1" />
            ::
                            <asp:LinkButton ID="btnLoeschen" runat="server"
                                Text="Delete"
                                Font-Names="Verdana"
                                Font-Size="8pt"
                                OnClick="btnLoeschen_click" meta:resourcekey="btnLoeschenResource1" />
            ::
                            <asp:LinkButton ID="btnSitemapAddDel" runat="server"
                                Text="Add / Delete to / from Sitemap"
                                Font-Names="Verdana"
                                Font-Size="8pt"
                                OnClick="btnSitemapAddDel_click" meta:resourcekey="btnSitemapAddDelResource1" />
            &nbsp;
            <div class="row">
                <div class="col-md-2">
                    <asp:DropDownList ID="ddMaster" runat="server" CssClass="form-control"></asp:DropDownList>
                </div>
                <div class="col-md-1">
                    <asp:Button ID="btnMaster" runat="server" CssClass="btn btn-default" Text="Set" OnClick="btnMaster_Click" />
                </div>
                <div class="col-md-2">
                    <asp:DropDownList ID="ddTheme" runat="server" CssClass="form-control"></asp:DropDownList>
                </div>
                <div class="col-md-1">
                    <asp:Button ID="btnTheme" runat="server" CssClass="btn btn-default" Text="Set" OnClick="btnTheme_Click" />
                </div>
                <div class="col-md-2">
                    <asp:DropDownList ID="ddPagetyp2" runat="server" CssClass="form-control"></asp:DropDownList>
                </div>
                <div class="col-md-1">
                    <asp:Button ID="btnPagetyp2" runat="server" CssClass="btn btn-default" Text="Set" OnClick="btnPagetyp2_Click" />
                </div>
                <div class="col-md-2">
                    <asp:DropDownList ID="ddSprachen" runat="server" CssClass="form-control"></asp:DropDownList>
                </div>
                <div class="col-md-1">
                    <asp:Button ID="btnSprachen" runat="server" CssClass="btn btn-default" Text="Set" OnClick="btnSprachen_Click" />
                </div>
            </div>

        </div>
    </div>
</asp:Content>
